//扫雷

#include<stdio.h>
#include<stdlib.h>
#include<time.h>


void set_mine(char mine[9][9]);						//埋雷
void display(char show[9][9]);                      //显示
int Sweep(char mine[9][9], char show[9][9]);		//扫雷
int get_num(char mine[9][9], int x, int y);			//算雷

//埋雷
void set_mine(char mine[9][9])
{
	int count = 20;
	int x = 0;
	int y = 0;
	srand((unsigned)time(NULL));
	while (count>0)
	{
		x = rand() % 8 + 1;
		y = rand() % 8 + 1;
		if (mine[x][y] == '0')
		{
			mine[x][y] = '1';
			count--;
		}
	}
}

//显示
void display(char show[9][9])  
{	

	int i = 0;
	int j = 0;
	printf(" ");
	for (i = 1; i < 9 ; i++)
	{
		printf(" %d ", i);
	}
	printf("\n");
	for (i = 1; i < 9 ; i++)
	{
		printf("%d", i);
		for (j = 1; j < 9 ; j++)
		{
			printf(" %c ", show[i][j]);
		}
		printf("\n");
	}
}
 


//算雷
int get_num(char mine[9][9], int x, int y)
{
	int count = 0;
	if (mine[x - 1][y - 1] == '1')//左上方
	{
		count++;
	}
	if (mine[x - 1][y] == '1')//左边
	{
		count++;
	}
	if (mine[x - 1][y + 1] == '1')//左下方
	{
		count++;
	}
	if (mine[x][y - 1] == '1')//上方
	{
		count++;
	}
	if (mine[x][y + 1] == '1')//下方
	{
		count++;
	}
	if (mine[x + 1][y - 1] == '1')//右上方
	{
		count++;
	}
	if (mine[x + 1][y] == '1')//右方
	{
		count++;
	}
	if (mine[x + 1][y + 1] == '1')//右下方
	{
		count++;
	}
	return  count;
}

//扫雷
int Sweep(char mine[9][9], char show[9][9])
{
	int count = 0;
	int x = 0;
	int y = 0;
	while (count!=44)
	{	
		printf("请输入坐标：\n");
		scanf("%d,%d", &x, &y);
		if (mine[x][y] == '1')
		{
			printf("你踩到雷了！\n");
			display(mine);
			return 0;
		}
		else
		{
			int ret = get_num(mine, x, y);
			show[x][y] = ret + '0';
			system("cls");
			display(show);
			count++;
		}
	}
	printf("恭喜你赢了！\n");
	display(mine);
	return 0;
}
 
int main()
{
	
	char mine[9][9];
	char show[9][9];
	int i = 0;
	int j = 0;
	for (i = 0; i < 9; i++)
	{
		for (j = 0; j < 9 ; j++)
		{
			mine[i][j] = '0';//定义数组内容=0
			show[i][j] = '*';//定义数组图像	show数组为char型
		}
	}

	set_mine(mine);			//埋雷
	display(show);			//显示					
	Sweep(mine,show);		//扫雷
	return 0;
}
 